package com.hzlj.position.locate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fz.common.base.dto.DeleteCommonDTO;
import com.hzlj.position.config.config.PositionConfig;
import com.hzlj.position.locate.cache.ReportInspectionCorrectConfigCache;
import com.hzlj.position.locate.common.common.PositionSimpleDTO;
import com.hzlj.position.locate.common.dto.reportInspectionCorrectConfig.ReportInspectionCorrectConfigUpsertDTO;
import com.hzlj.position.locate.common.model.ReportInspectionCorrectConfig;
import com.hzlj.position.locate.dao.ReportInspectionCorrectConfigDao;
import com.hzlj.position.locate.service.ReportInspectionCorrectConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.hzlj.position.config.config.IdConfig.POSITION_ID_GEN;

/**
 * 通信联络核查-核查-纠偏配置(ReportInspectionCorrectConfig)表服务接口
 *
 * @author lifh
 * @date 2023-07-26 15:57:03
 */
@Slf4j
@Service
public class ReportInspectionCorrectConfigServiceImpl implements ReportInspectionCorrectConfigService {
    @Resource
    private PositionConfig positionConfig;
    @Resource
    private ReportInspectionCorrectConfigDao reportInspectionCorrectConfigDao;
    @Resource
    private ReportInspectionCorrectConfigCache reportInspectionCorrectConfigCache;

    @Override
    public PositionSimpleDTO match(String jzId, String longitude, String latitude) {
        return reportInspectionCorrectConfigCache.match(jzId, longitude, latitude, positionConfig.getCorrect().getMatchDistance());
    }

    /**
     * 新增数据
     *
     * @param dto 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String saveReportInspectionCorrectConfig(ReportInspectionCorrectConfigUpsertDTO dto) {
        ReportInspectionCorrectConfig upsert = BeanUtil.copyProperties(dto, ReportInspectionCorrectConfig.class);
        upsert.setUpdateTime(new Date());
        if (ObjectUtil.isEmpty(dto.getId())) {
            upsert.setId(POSITION_ID_GEN.nextIdStr());
            this.reportInspectionCorrectConfigDao.save(upsert);
            log.info("【新增数据成功】:ReportInspectionCorrectConfig={}", upsert);
        } else {
            this.reportInspectionCorrectConfigDao.updateById(upsert);
            log.info("【更新数据成功】:ReportInspectionCorrectConfig={}", upsert);
        }
        //保存到缓存中
        reportInspectionCorrectConfigCache.save(upsert.getId(), upsert.getJzId(), dto.getCorrectOriginalPosition());
        return upsert.getId();
    }

    /**
     * 删除数据
     *
     * @param dto 实例对象
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteReportInspectionCorrectConfig(DeleteCommonDTO<String> dto) {
        List<ReportInspectionCorrectConfig> configs = this.reportInspectionCorrectConfigDao.listByIds(dto.toIds());
        boolean result = this.reportInspectionCorrectConfigDao.deleteReportInspectionCorrectConfig(dto);
        if (result) {
            //删除缓存
            for (ReportInspectionCorrectConfig config : configs) {
                reportInspectionCorrectConfigCache.delete(config.getId(), config.getJzId());
            }
            log.info("【删除数据成功】:PositionCorrectConfig={}", dto);
        }
        return result;
    }
}
